package atdd.kata.core;

import atdd.kata.core.domain.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import javax.sql.DataSource;
import java.util.UUID;

public class HrFixtures {

    public static Person vasyaPupkin() {
        return new Person("Vasya", "Pupkin", "Ivanovich", "Developer");
    }

    public static Person chuckSmith() {
        return new Person("Chuck", "Smith", "John", "QA Engineer");
    }

    public static Person emptyPerson() {
        return new Person();
    }

    public static MockHttpServletRequest chuckSmithRequest() {
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setRequestURI("/person/create");
        request.setParameter("firstname", "Chuck");
        request.setParameter("lastname", "Smith");
        request.setParameter("middlename", "John");
        request.setParameter("jobtitle", "QA Engineer");
        request.setMethod("POST");
        return request;
    }

    public static MockHttpServletRequest emptyRequestToCreateNewPerson() {
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setRequestURI("/person/create");
        request.setMethod("GET");
        return request;
    }

    public static MockHttpServletRequest emptyRequestToLandOnPersonList() {
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setRequestURI("/person/list");
        request.setMethod("GET");
        return request;
    }

    public static MockHttpServletResponse emptyResponse() {
        return new MockHttpServletResponse();
    }

    public static void addPersonalitiesPersistentContext(ApplicationContext applicationContext, Person... persons) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(applicationContext.getBean(DataSource.class));
        StringBuilder sql = new StringBuilder("");
        for (Person person : persons) {
            sql.append(String.format("insert into person (id, first_name, last_name, middle_name, job_title) values ('%s', '%s', '%s', '%s', '%s');",
                    UUID.randomUUID(), person.getFirstName(), person.getLastName(), person.getMiddleName(), person.getJobTitle()));
        }
        jdbcTemplate.update(sql.toString());
    }
}
